package ui.forms;

import com.sun.lwuit.*;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.GridLayout;
import javax.microedition.midlet.MIDlet;
import utils.Config;

/**
 * Display the menu screens options and takes user to the opted screen
 * @author Shayan
 */
public class MainMenu extends Form implements ActionListener {

    private static Command cmdExit;
    private static Command cmdLogout;
    public Context parent;
    private String[] items = new String[]{
        "Diary",
//        "Target",
//        "BMI",
        "Add Food",
        "Transfer Data",
//        "View Report"
    };

    MainMenu(final MIDlet parent) {
        setTitle("Anti Obesity Application");

        int width = Display.getInstance().getDisplayWidth();
        int elementWidth = 0;


        for (int i = 0; i < items.length; i++) {
            String name = items[i];

            Image selectedImage = Context.mainResource.getImage(name + "_sel");
            Image unselectedImage = Context.mainResource.getImage(name + "_unsel");

            final Button btn = new Button(name, unselectedImage);
            btn.setRolloverIcon(selectedImage);
            btn.setAlignment(Label.CENTER);
            btn.setTextPosition(Label.BOTTOM);
            btn.setUIID("DemoButton");

            addComponent(btn);
            btn.addActionListener(this);

            elementWidth = Math.max(btn.getPreferredW(), elementWidth);
        }

        //Calculate the number of columns for the GridLayout according to the
        //screen width
        int cols = width / elementWidth;
        int rows = items.length / cols;

        this.setLayout(new GridLayout(rows, cols));

        cmdExit = new Command("Exit") {

            public void actionPerformed(ActionEvent evt) {
                //Dialog.show("Id", Config.getRememberedUserId(), "OK", "");
                parent.notifyDestroyed();
            }
        };

        cmdLogout = new Command("Logout") {

            public void actionPerformed(ActionEvent evt) {
                try {
                    Config.setRememberedUserId("");
                    LoginForm form = new LoginForm(parent);
                    form.show();
                } catch (Exception ex) {
                    ex.printStackTrace();
                    Dialog.show("Error", ex.toString(), "OK", "");
                }
            }
        };
        this.addCommand(cmdExit);
        this.addCommand(cmdLogout);
        this.setBackCommand(cmdExit);
    }

    public void actionPerformed(ActionEvent e) {
        Button btn = (Button) e.getSource();
        if (btn == null) {
            return;
        }
        Form form = null;
        if (btn.getText().equals("Diary")) {
            form = new OldDiaryForm(this);
        }
//        if (btn.getText().equals("Target")) {
//            form = new TargetForm(this);
//        }
//        if (btn.getText().equals("BMI")) {
//            form = new BMIForm(this);
//        }
        if (btn.getText().equals("Add Food")) {
            form = new AddFoodForm(this);
        }
        if (btn.getText().equals("Transfer Data")) {
            form = new TransferDataForm(this);
        }
//        if (btn.getText().equals("View Report")) {
//            form = new ViewReportForm(this);
//        }

        if (form != null) {
            form.show();
        }
    }
}


